package com.epower.bo.epoweruserword.impl;

import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.epower.base.BaseBO;
import com.epower.bo.epower.commons.Constants;
import com.epower.bo.epoweruserword.EpowerUserWordService;
import com.epower.dao.epowerbook.EpowerBookDAO;
import com.epower.dao.epoweruserword.EpowerUserWordDAO;
import com.epower.dataobject.EpowerBookDO;
import com.epower.dataobject.EpowerUserWordDO;
import com.epower.dataobject.EpowerWordDO;

import java.util.ArrayList;
import java.util.List;

/***
 * BO层实现类
 * 
 * @author QuickDev
 * 
 */
@Scope("singleton")
@Component("epowerUserWordBO")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EpowerUserWordServiceImpl extends BaseBO implements EpowerUserWordService {
	private EpowerUserWordDAO epowerUserWordDAO;
	private EpowerBookDAO epowerBookDAO;

	/***
     * 
     */
	@Override
	public EpowerUserWordDO getEpowerUserWordByOption(EpowerUserWordDO epowerUserWord) {
		return epowerUserWordDAO.getEpowerUserWordByOption(epowerUserWord);
	}

	/***
     * 
     */
	@Override
	public List<EpowerUserWordDO> getEpowerUserWordListByOption(EpowerUserWordDO epowerUserWord) {
		return epowerUserWordDAO.getEpowerUserWordListByOption(epowerUserWord);
	}

	@Override
	public List<EpowerUserWordDO> getEpowerUserWordListPageByOption(EpowerUserWordDO epowerUserWord) {
		int totalItem = getEpowerUserWordCountByOption(epowerUserWord);
		epowerUserWord.setTotalItem(totalItem);
		return epowerUserWordDAO.getEpowerUserWordListByOption(epowerUserWord);
	}

	/***
     * 
     */
	@Override
	public EpowerUserWordDO getEpowerUserWordById(Integer id) {
		return epowerUserWordDAO.getEpowerUserWordById(id);
	}

	/***
     * 
     */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean deleteEpowerUserWordById(Integer id) {
		return epowerUserWordDAO.deleteEpowerUserWordById(id);
	}

	/***
     * 
     */
	@Override
	public Integer getEpowerUserWordCountByOption(EpowerUserWordDO epowerUserWord) {
		return epowerUserWordDAO.getEpowerUserWordCountByOption(epowerUserWord);
	}

	/***
     * 
     */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean updateEpowerUserWordByOption(EpowerUserWordDO epowerUserWord) {
		return epowerUserWordDAO.updateEpowerUserWordByOption(epowerUserWord);
	}

	@Override
	public boolean updateEpowerUserWordLevel(int bookid,int wordid,int userid,int level) {
		return epowerUserWordDAO.updateEpowerUserWordLevel(bookid,wordid,userid,level);
	}
	
	/***
     * 
     */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public Integer addEpowerUserWord(EpowerUserWordDO epowerUserWord) {
		try {
			Integer id = epowerUserWordDAO.addEpowerUserWord(epowerUserWord);
			return id;
		} catch (Exception e) {
			throw new RuntimeException("service is error");
		}
	}

	/***
	 * 
	 * @getter setter
	 */
	public EpowerUserWordDAO getEpowerUserWordDAO() {
		return epowerUserWordDAO;
	}

	@Resource
	public void setEpowerUserWordDAO(EpowerUserWordDAO epowerUserWordDAO) {
		this.epowerUserWordDAO = epowerUserWordDAO;
	}

	@Override
	public String getWordImgUrl(Integer wordId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<EpowerWordDO> getEpowerWordsByOption(EpowerUserWordDO epowerUserWord) {
		return null;
	}

	@Override
	public List<EpowerBookDO> getBookListByUserId(EpowerUserWordDO query) {
		query.setStatus(Constants.STATUS_OK);
		List<EpowerBookDO> bookList = epowerUserWordDAO.getBookListByUserId(query);
		for (EpowerBookDO book : bookList) {
			List<EpowerBookDO> childBookList = new ArrayList<EpowerBookDO>();
			query.setBookid(book.getId());
			List<Integer> list = epowerUserWordDAO.getBookChildList(query);
			int allStudyNum = 0;
			for (int i : list) {
				EpowerBookDO b = epowerBookDAO.getEpowerBookById(i);
				if (b != null) {
					query.setBookid(i);
					query.setLevel(Constants.USER_WORD_LEVEL_FAMILIAR);
					b.setStudyNum(epowerUserWordDAO.getEpowerUserWordCountByOption(query));
					allStudyNum += b.getStudyNum();
					childBookList.add(b);
				}
			}
			book.setChildBookList(childBookList);
			book.setStudyNum(allStudyNum);
		}
		return bookList;
	}

	@Override
	public List<EpowerWordDO> getUserCollections(EpowerUserWordDO query) {
		return epowerUserWordDAO.getUserCollections(query);
	}

	/**
	 * 获取某个人在某个课本中的熟悉词数
	 */
	@Override
	public WordCountResult getFamiliarWordCountAndStrangeWordCount(int bookid, int userid) {
		int familiarWordCount = epowerUserWordDAO.getFamiliarWordCountByBookid(bookid, userid);
		int strangeWordCount = epowerUserWordDAO.getStrangeWordCountByBookid(bookid, userid);
		return new WordCountResult(familiarWordCount,strangeWordCount);
	}

	@Override
	public int queryEpowerUserWordCount(int wordid, int userid,int bookid) {
		return epowerUserWordDAO.queryEpowerUserWordCount(wordid,userid,bookid);
	}
	
	public void setEpowerBookDAO(EpowerBookDAO epowerBookDAO) {
		this.epowerBookDAO = epowerBookDAO;
	}

	@Override
	public List<EpowerWordDO> getUserWordsByUserId(EpowerUserWordDO query) {
		return epowerUserWordDAO.getUserWordsByUserId(query);
	}

	public class WordCountResult {
		public int familiarWordCount;
		public int strangeWordCount;

		public WordCountResult(int familiarWordCount, int strangeWordCount) {
			this.familiarWordCount = familiarWordCount;
			this.strangeWordCount = strangeWordCount;
		}
	}

	@Override
	public boolean releaseCollections(Integer userId, Integer wordId) {
		return epowerUserWordDAO.releaseCollections(userId, wordId) > 0 ? true : false;

	}

}
